package com.cy.sys.log.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.cy.sys.log.Dao.SysMenusDao;
import com.cy.sys.log.Dao.SysRoleMenuDao;
import com.cy.sys.log.Dao.SysRoleUserDao;
import com.cy.sys.log.Dao.SysUserDao;
import com.cy.sys.log.service.SysMenusService;
import com.cy.sys.log.service.exception.ServiceException;
import com.cy.sys.pojo.NodeMenu;
import com.cy.sys.pojo.SysMenus;
import com.cy.sys.pojo.SysRoleMenus;
import com.cy.sys.pojo.SysmenuId;

@Service
public class SysMenusServiceimpl implements SysMenusService{
	@Autowired
	private SysMenusDao sysmenus;
	@Autowired
	private SysRoleMenuDao sysroledao;
	@Autowired
	private SysUserDao sysuserroledao;
	@Autowired
	private SysRoleUserDao sysmenroledao;
	
	
	@Override
	public List<SysmenuId> findMenusByIds(Integer id) {
		if(id==null)
			throw new IllegalArgumentException("参数无效");
		//根据id查询角色id
		 List<Integer> roleids = sysuserroledao.findRoleuserById(id);
		//给予角色id查询菜单信息
		 List<Integer> menids = sysmenroledao.findRolemensintger(roleids);
		 //封装返回对象	
		return sysmenus.findMenusByIds(menids);
	}
	
	/**
	 * 	修改
	 */
	@Override
	public int doupdateObject(SysMenus sysmenu) {
		if(sysmenu.getName()==null||sysmenu.getUrl()==null||sysmenu.getSort()<0)
			throw new ServiceException("对不起你有参数不合法不能新增操作");
		return sysmenus.doupdateObject(sysmenu);
	}
	
	/**
	 * 	新增
	 */
	@Override
	public int doinsertObject(SysMenus sysmenu) {
		//判断是否有值
		if(sysmenu.getName()==null||sysmenu.getUrl()==null||sysmenu.getSort()<0)
			throw new ServiceException("对不起你有参数不合法不能新增操作");
		
		return sysmenus.doinsertObject(sysmenu);
	}
	
	
	/**
	 *	树结构
	 */
	@Override
	public List<NodeMenu> findzTree() {
		//校验...
		List<NodeMenu> nodeztree = sysmenus.findzTree();
		if(nodeztree==null) throw new ServiceException("查询为空没有对应");
		return nodeztree;
	}
	
	/**
	 * 	查询
	 */
	@Cacheable(value = "menuscahe")
	@Override
	public List<Map<String,Object>> getMenusfindObject() {
		List<Map<String,Object>> menusList = 
				sysmenus.getMenusfindObject();
		System.out.println("查询数据库");
		if(menusList==null||menusList.size()<0)
			throw new ServiceException("没有找到参数");
		return menusList;
	}

	/**
	 *	删除
	 */
	@CacheEvict(value = "menuscahe",allEntries = true,beforeInvocation = false)
	@Override
	public int deleteObject(Integer id) {
		//校验参数
		if(id==null||id<0) new IllegalArgumentException("您的参数值不合法");
		//判断是否有子菜单
		int row = sysmenus.queryMenuprinId(id);
		System.out.println(row);
		if(row>0) throw new ServiceException("你选中的有子菜单先删除子菜单");
		
		
		//执行菜单角色删除
		sysroledao.deleteMenuId(id);
		
		//删除自身信息
		int ros = sysmenus.deleteObject(id);
		if(ros==0) throw new ServiceException("你的信息可能不存在");
		return ros;
	}
	
}
